package org.noobie.photopicker.ui;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.LinearLayout;

import com.github.chrisbanes.photoview.PhotoView;

import org.noobie.photopicker.R;
import org.noobie.photopicker.entity.Photo;
import org.noobie.photopicker.util.LoadUtils;

/**
 * 预览大图
 */
public class PhotoPreview extends LinearLayout{

//	private ProgressBar pbLoading;
	private PhotoView ivContent;
	private OnClickListener mClickListener;

	public PhotoPreview(Context context) {
		super(context);
		LayoutInflater.from(context).inflate(R.layout.view_photopreview, this, true);

		ivContent = findViewById(R.id.iv_content_vpp);

//		ivContent.setOnClickListener(this);
		//photoView的已经拦截了点击事件，单击需自己做判断
		ivContent.setOnPhotoTapListener((view, x, y) -> {
            if (mClickListener != null)
                mClickListener.onClick(ivContent);
        });
	}

	public PhotoPreview(Context context, AttributeSet attrs, int defStyle) {
		this(context);
	}

	public PhotoPreview(Context context, AttributeSet attrs) {
		this(context);
	}

	public void loadImage(Photo Photo) {
		loadImage("file://" + Photo.getFilePath());
	}

	private void loadImage(String path) {
		LoadUtils.loadImage(getContext(),path, ivContent);
	}

	@Override
	public void setOnClickListener(OnClickListener l) {
		this.mClickListener = l;
	}
}
